package com.cyt.work.site.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.site.dao.SchoolDao;
import com.cyt.work.site.model.po.School;
import com.cyt.work.site.model.vo.SchoolVo;
import com.cyt.work.site.service.SchoolService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 微官网-学校简介Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-8 18:37:06
 */
@Service("schoolService")
public class SchoolServiceImpl extends BaseServiceImpl<School> implements SchoolService {

    @Resource
    private SchoolDao schoolDao;
    @Resource
    private SysFileService sysFileService;

    @Autowired
    public void setBaseDao(SchoolDao schoolDao) {
        this.baseDao = schoolDao;
    }

    /**
     * 查询对象
     *
     * @param schoolVo 对象
     * @return SchoolVo
     * @author rong yuan
     * @date 2019-7-8 18:37:06
     */
    @Override
    public SchoolVo selectSchool(SchoolVo schoolVo) {
        SchoolVo vo = schoolDao.selectByCondition(schoolVo);
        if (vo != null) {
            SysFileVo sysFileVo;
            if (StringUtils.isNotBlank(vo.getPicture())) {
                sysFileVo = new SysFileVo(vo.getPicture());
                vo.setPictureList(sysFileService.listExample(sysFileVo));
            }
            if (StringUtils.isNotBlank(vo.getHonour())) {
                sysFileVo = new SysFileVo(vo.getHonour());
                vo.setHonourList(sysFileService.listExample(sysFileVo));
            }
        } else {
            vo = new SchoolVo();
        }
        return vo;
    }

    /**
     * 添加或更新
     *
     * @param schoolVo 参数接受对象
     * @param sysUser  当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-8 18:37:06
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(SchoolVo schoolVo, SysUser sysUser) {
        if (StrUtil.isEmpty(schoolVo.getId())) {
            School school = new School(sysUser);
            school.setApplyId(schoolVo.getApplyId());
            school.setName(schoolVo.getName());
            school.setLeader(schoolVo.getLeader());
            school.setSituation(schoolVo.getSituation());
            school.setHistory(schoolVo.getHistory());
            school.setThought(schoolVo.getThought());
            school.setPicture(schoolVo.getPicture());
            school.setScenery(schoolVo.getScenery());
            school.setHonour(schoolVo.getHonour());
            school.setLongitude(schoolVo.getLongitude());
            school.setLatitude(schoolVo.getLatitude());
            school.setContact(schoolVo.getContact());
            int num = schoolDao.insert(school);
            if (num > 0) {
                return new ReturnResult(ErrorCode.CODE_0, school.getId(), BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            School school = schoolDao.selectByPrimaryKey(schoolVo.getId());
            school.setApplyId(schoolVo.getApplyId());
            school.setName(schoolVo.getName());
            school.setLeader(schoolVo.getLeader());
            school.setSituation(schoolVo.getSituation());
            school.setHistory(schoolVo.getHistory());
            school.setThought(schoolVo.getThought());
            school.setPicture(schoolVo.getPicture());
            school.setScenery(schoolVo.getScenery());
            school.setHonour(schoolVo.getHonour());
            school.setLongitude(schoolVo.getLongitude());
            school.setLatitude(schoolVo.getLatitude());
            school.setContact(schoolVo.getContact());
            school.setUpdateTime(new Date());
            school.setUpdateUser(sysUser.getId());
            int num = schoolDao.updateByPrimaryKey(school);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }
}